from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from time import sleep

from LQT.selenium_api.selenium_base import WebBase

class YqwWeb(WebBase):
    def login(self):
        if self.online: return

        if 'www.yqw28.cn/pages/#/home' in self.browser.current_url:
            self.browser.refresh()
        else:
            self.browser.get('https://www.yqw28.cn/pages/#/home')

        try:
            # 等待root加载
            self.wait.until(
                EC.presence_of_element_located((By.ID,'root'))
            )
            print('root node loaded.')
            sleep(1)

            # 等待弹窗并关闭
            try:
                submit=self.wait.until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,'body > div:nth-child(10) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-confirm-btns > button'))
                )
                submit.click()
                print('关闭弹窗...')
                sleep(1)
            except:
                print('没有弹窗...')

            # 自动填表
            input_user=self.browser.find_element_by_css_selector('#userName')
            input_password=self.browser.find_element_by_css_selector('#password')
            submit = self.browser.find_element_by_css_selector('#\/home > div.ant-layout > div:nth-child(2) > div > div > div > div > div > div > div:nth-child(1) > div > div > div.ant-col-7 > div > div > div > div.ant-tabs-content.ant-tabs-content-animated > div.ant-tabs-tabpane.ant-tabs-tabpane-active > form > div.ant-row.ant-form-item.px-sm.py-xs > div > div > span > button')
            input_user.send_keys('13568739782')
            input_password.send_keys('123456xb')
            submit.click()
            sleep(2)
            return self.verify()
        except:
            print('网页加载超时...')
            sleep(1)
            return self.login()

    def verify(self):
        if 'www.yqw28.cn' in self.browser.current_url:
            self.browser.refresh()
        else:
            self.browser.get('https://www.yqw28.cn/pages/#/home')

        # 欢迎您来到一起玩28网站
        try:
            self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,'#\/home > div.ant-layout > div.ant-layout-header > div.bgc-grey-3'))
            )
            print('header node loaded')
            sleep(1)

            # 获取用户名
            try:
                use_name = self.browser.find_element_by_css_selector('#\/home > div.ant-layout > div.ant-layout-header > div.bgc-grey-3 > div > div.ant-col-8.ta-left > p > span:nth-child(3) > em')
                print('验证成功！ '+use_name.text)
                self.online=True

                return True
            except:
                print('验证失败！')
                self.online=False

                return False
        except:
            print('网页加载超时...')
            sleep(1)
            return self.verify()

    def get_curr_data(self):
        if not self.online: return None
        result=[]
        if 'www.yqw28.cn/pages/#/lottery/l28/js28/betting' in self.browser.current_url:
            self.browser.refresh()
        else:
            self.browser.get('https://www.yqw28.cn/pages/#/lottery/l28/js28/betting')

        # 获取当前开奖信息
        try:
            curr_issue = self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,'#\/lottery\/l28\/js28\/betting > div.ant-layout > div:nth-child(2) > div > div > div > div > div > div > div.my-lg > div.ant-row.bgc-grey-1 > table > tbody > tr > td:nth-child(2) > div > span.fs-md > em'))
            )
            #print(curr_issue.text)
            result.append(curr_issue.text)
            try:
                number=self.wait.until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,'#\/lottery\/l28\/js28\/betting > div.ant-layout > div:nth-child(2) > div > div > div > div > div > div > div.my-lg > div.ant-row.bgc-grey-1 > table > tbody > tr > td:nth-child(2) > div > span.fs-md > span > em'))
                )
                if '正在开奖' in number.text:
                    print('开奖中...')
                    sleep(1)
                    return self.get_curr_data()
                else:
                    #print(number.text)
                    result.append(number.text)
                    return result
            except:
                print('开奖中...')
                sleep(1)
                return self.get_curr_data()
        except:
            print('错误...')
            sleep(1)
            return self.get_curr_data()

    def get_all_1000_data(self):
        if not self.online: return None
        result=[]
        if 'https://www.yqw28.cn/pages/#/lottery/l28/js28/trend' in self.browser.current_url:
            self.browser.refresh()
        else:
            self.browser.get('https://www.yqw28.cn/pages/#/lottery/l28/js28/trend')

        # 获取当前开奖信息
        try:
            curr_issue = self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,'#\/lottery\/l28\/js28\/trend > div.ant-layout > div:nth-child(2) > div > div > div > div > div > div > div.my-lg > div.ant-row.bgc-grey-1 > table > tbody > tr > td:nth-child(2) > div > span.fs-md > em'))
            )
            issue=int(curr_issue.text)
            count_input=self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,'#\/lottery\/l28\/js28\/trend > div.ant-layout > div:nth-child(2) > div > div > div > div > div > div > div.mb-lg > div.px-lg.fs-md.tc-grey-9 > input'))
            )
            count_input.send_keys(1000)
            click_wait=self.wait.until(
                EC.presence_of_element_located((By.CSS_SELECTOR,'#\/lottery\/l28\/js28\/trend > div.ant-layout > div:nth-child(2) > div > div > div > div > div > div > div.mb-lg > div.px-lg.fs-md.tc-grey-9'))
            )
            click_wait.click()
            sleep(10)
            try:
                numbers=self.wait.until(
                    EC.presence_of_all_elements_located((By.CLASS_NAME,'result-ball'))
                )
                i=0
                for number in numbers:
                    if i!=0:
                        temp_result=[]
                        temp_result.append(str(issue-i+1))
                        temp_result.append(number.text)
                        result.append(temp_result)
                    i+=1
                return result
            except:
                sleep(1)
                return self.get_all_1000_data()
        except:
            sleep(1)
            return self.get_all_1000_data()

    def post(self,issue):
        if not self.online: return False

        url='https://www.yqw28.cn/pages/#/lottery/l28/js28/betting/detail/'+issue
        self.browser.get(url)

        try:
            # 等待root加载
            self.wait.until(
                EC.presence_of_element_located((By.ID,'root'))
            )
            print('root node loaded.')
            sleep(1)

            # 等待弹窗并关闭
            try:
                submit=self.wait.until(
                    EC.presence_of_element_located((By.CSS_SELECTOR,'body > div:nth-child(10) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-confirm-btns > button'))
                )
                submit.click()
                print('关闭弹窗...')
                sleep(1)
            except:
                print('没有弹窗...')
                pass

            # 进行投注
            options=self.wait.until(
                EC.presence_of_all_elements_located((By.CLASS_NAME,'ant-input-sm'))
            )
            print(len(options))  # 应该是28
            for option in options:
                option.click()
                option.send_keys('1')  # 这里设置具体投注金额

            sleep(1)

            submit_posts=self.wait.until(
                EC.presence_of_all_elements_located((By.CLASS_NAME,'ant-btn-lg'))
            )
            print(len(submit_posts))  # 应该是2
            submit_posts[1].click()

            sleep(1)

            submit_posts=self.wait.until(
                EC.presence_of_all_elements_located((By.CLASS_NAME,'ant-btn-primary'))
            )
            print(len(submit_posts))  # 应该是22
            submit_posts[21].click()

            sleep(1)

            # 处理提交投注后的信息
        except:
            print('网页加载超时...')
            sleep(1)
            self.post(issue)




        
        
        
            
